﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyBet.Helpers;

namespace MyBet.Controls
{
    public partial class ModifyAccountDialog : ChildWindow
    {
        public ModifyAccountDialog()
        {
            InitializeComponent();
            UpdateCulture();
            EmailText.Text = App.CurrentUser.Email;
            this.Title = String.Format(Helpers.LangHelper.GetString("ChangePasswordTitle"), App.CurrentUser.UserName);
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(PasswordText1.Password.Trim()))
            {
                MessageBox.Show(Helpers.LangHelper.GetString("OldPasswordNotEmpty"));
                return;
            }
            if (String.IsNullOrEmpty(PasswordText2.Password.Trim()))
            {
                MessageBox.Show(Helpers.LangHelper.GetString("NewPasswordNotEmpty"));
                return;
            }
            if (PasswordText2.Password.Trim().Length < 6)
            {
                MessageBox.Show(Helpers.LangHelper.GetString("NewPasswordAtLeastSixCharacters"));
                return;
            }
            if (PasswordText1.Password.Trim() == PasswordText2.Password.Trim())
            {
                MessageBox.Show(Helpers.LangHelper.GetString("NewPasswordDifferentThanOldPassword"));
                return;          
            }
            SecurityServiceReference.SecurityServiceClient securitySvc = new SecurityServiceReference.SecurityServiceClient();
            securitySvc.ChangeUserPasswordCompleted += new EventHandler<SecurityServiceReference.ChangeUserPasswordCompletedEventArgs>(securitySvc_ChangeUserPasswordCompleted);
            securitySvc.ChangeUserPasswordAsync(App.CurrentUser.UserName, PasswordText1.Password.Trim(), PasswordText2.Password.Trim());
            BusyControl.BusyContent = Helpers.LangHelper.GetString("AccountChangeInProgress");
            BusyControl.IsBusy = true;
        }

        void securitySvc_ChangeUserPasswordCompleted(object sender, SecurityServiceReference.ChangeUserPasswordCompletedEventArgs e)
        {
            if (!e.Result)
            {
                MessageBox.Show(Helpers.LangHelper.GetString("ChangePasswordError"));
            }
            else
            {
                MessageBox.Show(Helpers.LangHelper.GetString("ChangePasswordOK"));
                this.DialogResult = true;
            }
            BusyControl.IsBusy = false;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public void UpdateCulture()
        {
            EmailLabel.Content = Helpers.LangHelper.GetString("Email");
            PasswordLabel1.Content = Helpers.LangHelper.GetString("OldPassword");
            PasswordLabel2.Content = Helpers.LangHelper.GetString("NewPassword");
            CancelButton.Content = Helpers.LangHelper.GetString("Cancel");
        }

    }
}

